import config from './config';
// let base = config.uploadFile;
let base = config.isDev ? ''  : config.uploadFile;
let urlBase = 'api/upload/upload';
let randToken = null;

export default {
  uploadFile: (params,url=urlBase) => {
    return new Promise((resolve,reject)=>{
      randToken = localStorage.getItem('randToken')
      // console.log("randToken",randToken)
      if(randToken){
        params.token = randToken
      }
      var xhr = new XMLHttpRequest();
      xhr.open('POST', `${base}${url}?token=${params.token}`,);
      //设置请求头
      xhr.setRequestHeader('apptype','admin');

      xhr.send(params);
      xhr.onreadystatechange = function() {

        // resolve(xhr)
        if (xhr.readyState === 4) {
          if (/^(2|3)\d{2}$/.test(xhr.status)) {
            let respon = JSON.parse(xhr.responseText);
           // console.log("responseText",respon)
           if (respon && respon.token) {
             randToken = respon.token;
             // console.log("randToken",randToken)
             localStorage.setItem('randToken', randToken);
           }
           resolve(respon);
          } else {
           reject({ msg: "请求已中断" });
          }
        }
      };
    })
  },
}
